package com.example.lmy.okgotestdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button Get;
    private Button Post;
    private Button Put;
    private Button Delete;
    HttpHeaders headers = new HttpHeaders();
    private Button okgo;








    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Get =  findViewById(R.id.Get);
        Post = findViewById(R.id.Post);
        Put =  findViewById(R.id.Put);
        Delete =  findViewById(R.id.Delete);
        Get.setOnClickListener(this);
        Post.setOnClickListener(this);
        Put.setOnClickListener(this);
        Delete.setOnClickListener(this);
        okgo = (Button) findViewById(R.id.okgo);
        okgo.setOnClickListener(this);

        headers.put("Content-Type", "application/json");
        headers.put("X-Requested-With", "XMLHttpRequest");
        headers.put("X-UID", "123456789");
        headers.put("X-ATOKEN","963258741");
        headers.put("X-CID","4guv7ny3sgzk");




    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Get:
                OkGo.<String>get("http://192.168.1.88:9090/app/b/companies/4guv7ny3sgzk/customers")//
                        .tag(this)//
                        .headers(headers)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                //注意这里已经是在主线程了
                                String data = response.body();//这个就是返回来的结果
                                Log.d("GetData","返回的数据"+data);
                            }
                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                            }
                        });

                break;
            case R.id.Post:
                HashMap<String, String> map = new HashMap<>();
                map.put("name", "liming");
                map.put("mobile", "13623899123");
                JSONObject jsonObject=new JSONObject(map);
                OkGo.<String>post("http://192.168.1.88:9090/app/b/companies/4guv7ny3sgzk/shareMembers")//
                        .tag(this)
                        .headers(headers)
                        .upJson(jsonObject)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                //注意这里已经是在主线程了
                                String data = response.body();//这个就是返回来的结果
                                Log.d("GetData","返回的数据"+data);

                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                            }
                        });


                break;
            case R.id.Put:
                HashMap<String, String> body = new HashMap<>();
                body.put("name", "测试");
                body.put("work", "测试OkGo");
                JSONObject jsonObject1=new JSONObject(body);
                OkGo.<String>put("http://192.168.1.88:9090/app/b/companies/4guv7ny3sgzk/customers/4kxefhxt2ps0")//
                        .tag(this)
                        .headers(headers)
                        .upJson(jsonObject1)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                //注意这里已经是在主线程了
                                String data = response.body();//这个就是返回来的结果
                                Log.d("GetData","返回的数据"+data);
                            }
                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                            }
                        });
                break;
            case R.id.Delete:
                break;
            case R.id.okgo:
                TestOkGoActivity.show(MainActivity.this);
                break;

        }
    }
}
